const Router = require('koa-router');
const router = new Router();
const tool = require("../tools/tool");
const {db, Op} = require("../models/db");
router.post('/dept/deptList', async ctx => {
    let tid = ctx.user.tenantId
    let sql = `select a.*,b.name manName from dept a left join admin b on a.manId=b.id where a.tenantId=${tid} and a.isDel=0  `
    let ret = tool.page(ctx, sql)
    return ret
})
router.post('/dept/delById', async ctx => {
    let ids = ctx.request.body.ids
    let c = await db.models['admin'].count({
        where: {
            tenantId: ctx.user.tenantId,
            deptId: {
                [Op.in]: ids
            }
        }
    })
    if(c > 0) {
        return  ctx.err('已使用部门，不能删除')
    }
    await db.models['dept'].destroy({
        where: {
            tenantId: ctx.user.tenantId,
            id: {
                [Op.in]: ids
            }
        }
    })
    return ctx.suc('已删除')
})

module.exports = router;